package org.zjx.app.action.task;

import org.zjx.analysis.analyze.game.GameAnalysis;
import org.zjx.analysis.analyze.window.impl.DialogueAnalyzer;
import org.zjx.analysis.analyze.window.impl.GiveNpcAnalyzer;
import org.zjx.app.action.route.BaseRouteAction;
import org.zjx.app.action.window.ClickSceneAction;
import org.zjx.app.action.window.DialogueAction;
import org.zjx.command.entity.screen.Address;
import org.zjx.command.entity.screen.Npc;
import org.zjx.command.enums.SlotType;
import org.zjx.window.action.Action;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class GiveNpcItemTaskAction extends Action {
    private final Npc npc;
    private final SlotType slotType;
    private final String itemName;


    public GiveNpcItemTaskAction(IWindow targetWindow, Npc npc, SlotType slotType, String itemName) {
        super(targetWindow);
        this.npc = npc;
        this.slotType = slotType;
        this.itemName = itemName;
    }

    @Override
    protected void doExecute() throws Exception {
        Address targetAddress = npc.getMainLocation();
        GameAnalysis gameAnalysis = new GameAnalysis(targetWindow);
        Address address = gameAnalysis.getAddress();
        if (!address.near(targetAddress, 5)) {
            targetWindow.submitAction(new BaseRouteAction(targetWindow, targetAddress));
        } else {
            targetWindow.actions().moveTo(200,100);
            targetWindow.flushScreenshot();
            GiveNpcAnalyzer giveNpcAnalyzer = new GiveNpcAnalyzer(targetWindow);
            if (!giveNpcAnalyzer.isOpened()) {
                List<Point> points = npc.getMovePath();
                List<Address> addressesList = new ArrayList<>();
                for (Point point : points) {
                    addressesList.add(new Address(npc.getSceneName(), point.x, point.y));
                }
                targetWindow.submitAction(new ClickSceneAction(targetWindow, addressesList, ClickSceneAction.ClickType.GIVE));
            } else {
                if (slotType == SlotType.INVENTORY && !giveNpcAnalyzer.chooseInventory()) {
                    targetWindow.actions().clickIn(giveNpcAnalyzer.getInventoryButtonRectangle()).delay(30)
                            .moveTo(200,50).delay(200);
                } else if (slotType == SlotType.QUEST_ITEMS && !giveNpcAnalyzer.chooseTask()) {
                    targetWindow.actions().clickIn(giveNpcAnalyzer.getTaskButtonRectangle()).delay(30)
                            .moveTo(200,50).delay(200);
                } else {
                    Rectangle itemRectangle = giveNpcAnalyzer.getItemIndexRectangle(0);
                    targetWindow.actions().clickIn(itemRectangle).delay(100)
                            .clickIn(giveNpcAnalyzer.getSureButtonRectangle()).delay(1000);
                    targetWindow.submitAction(new DialogueAction(targetWindow,null));
                    targetWindow.getState().getGameState().setCommand(null);
                    targetWindow.getState().getGameState().setGatherQuest(false);
                }
            }
        }
    }
}
